#include "HF_Lib.h"
#include <stdio.h>
#include <stdlib.h>
#include "BF_Defines.h"

void HF_Init(){
	BF_Init();
	SMM_Init();
}

int HF_CreateFile(char * fileName ){
	int fd;
	char * header_block;
	int *blockNum;
	header_block = malloc(BF_BLOCK_SIZE);
	if( (BF_CreateFile(fileName)) < 0){
		HF_errno = HFE_BFERROR;
		BF_PrintError("BF error in HF_CreateFile");
	}
	if(( fd = BF_OpenFile(fileName)) < 0){
		HF_errno = HFE_BFERROR;
		BF_PrintError("BF error in HF_CreateFile");
	}
	if(( BF_AllocBlock(fd, &blockNum, header_block)) < 0){
	   HF_errno = HFE_BFERROR;
	   BF_PrintError("BF error in HF_CreateFile");
	}
	//memset??
	if(( BF_UnpinBlock(fd, blockNum, TRUE) ) < 0){
		HF_errno = HFE_BFERROR;
		BF_PrintError("BF error in HF_CreateFile");
	}
	if(( BF_CloseFile(fd)) < 0 ){
		HF_errno = HFE_BFERROR;
		BF_PrintError("BF error in HF_CreateFile");
	}
	return HF_errno = HFE_OK;
}



int HF_DerstroyFile(char * fileName){
	if(( BF_DestroyFile(fileName)) < 0 ){
		HF_errno = HFE_BFERROR;
		BF_PrintError("BF error in HF_DestroyFile");
	}
	return HF_errno = HFE_OK;
}

int HF_OpenFile(char * fileName){
	
	int fd;
	if( ( HF_errno = BF_OpenFile(fileName) ) < 0 ){
		return HF_errno;
	}
	fd = HF_errno;
	HF_errno = HFE_OK;
	return fd;
	
	
}


int HF_CloseFile(int fileDesc) {
	if( SMM_hasOpenScans(fileDesc) ){
		return HF_errno = HAS_OPEN_SCANS;
	}
	if( ( BF_CloseFile(fileDesc) ) < 0 ){
		BF_PrintError("BF error in HF_CloseFile");
		return HF_errno = HFE_BFERROR;
	}
	
	return HF_errno = HFE_OK;
	
}

int HF_InsertRec(int fileDesc, char * record, int recordSize){

	int recId; 
	
	if(recordSize < 0 || recordSize > BF_BLOCK_SIZE){
		return HF_errno = INVALID_RECORD_SIZE;
	}
	
	if(( HF_errno =  RMM_InsertRec(fileDesc, record, recordSize) )<0){
		return HF_errno;
	}
	else
		return recId=HF_errno;
}

int HF_DeleteRec(int fileDesc, int recId, int recordSize){
								
	if ( (HF_errno=RMM_DeleteRec(fileDesc, recId, recordSize)) < 0 )
		return HF_errno;
	else
		return HF_errno=HFE_OK;
}

int HF_GetFirstRec(int fileDesc, char* record, int recordSize){
	
	int recId;
	if ( (recId = RMM_GetFirstRec(fileDesc, record, recordSize)) < 0 )
		return HF_errno = recId;
	else{
		HF_errno = HFE_OK;
		return recId;
	}
}



int HF_GetNextRec(int fileDesc,int recId, char* record, int recordSize){\
	
	int rec_id;
	if ( (rec_id=RMM_GetNextRec(fileDesc,recId, record, recordSize)) < 0 ){
		return HF_errno = rec_id;
	}
	else{
		HF_errno = HFE_OK;
		return rec_id;
	}
}


int HF_GetThisRec(int fileDesc,int recId,  char* record, int recordSize){
	
	if ( (HF_errno=RMM_GetThisRec(fileDesc,recId, record, recordSize)) < 0 ){
		return HF_errno;
	}
	else{
		return HF_errno=HFE_OK;   // ok	
	}
	
}


int HF_OpenFileScan(int fileDesc,  int recordSize, char attrType,int attrLength,int attrOffset,int op,char *value){
	int scanDesc;
	
	if (( scanDesc = SMM_OpenFileScan(fileDesc, recordSize, attrType, attrLength, attrOffset, op, value) ) < 0){
		return HF_errno = scanDesc;
	}
	else{
		HF_errno = HFE_OK;
		return scanDesc;
	}
	
}

int HF_FindNextRec(int scanDesc, char * record){
	int recId;
	if(( recId = SMM_FindNextRec(scanDesc, record)) < 0){
		return HF_errno = recId;
	}
	else{
		HF_errno = HFE_OK;
		return recId;
	}
}

int HF_CloseFileScan(int scanDesc){
	if(( HF_errno = SMM_CloseFileScan(scanDesc)) < 0 ){
		return HF_errno;
	}
	else{
		return HF_errno = HFE_OK;
	}
}


void HF_PrintError(char * errString){
	switch (HF_errno) {
		case HFE_BFERROR:
			BF_PrintError(errString);
			break;
		case HFE_EOF:
			printf("%s\nHF_errno = -2 ->  End of file\n", errString);
			break;
		case HFE_INVALIDREC:
			printf("%s\nHF_errno = -3 ->  Invalid record\n", errString);
			break;
		case HFE_STFULL:
			printf("%s\nHF_errno = -4 ->  Scan table ful\n", errString);
			break;
		case HFE_INVALIDOP:
			printf("%s\nHF_errno = -5 ->  Invalid scan operator\n", errString);
			break;
		case BLOCK_FULL:
			printf("%s\nHF_errno = -6 ->  Block is Full\n", errString);
			break;
		case RMM_INVALID_BLOCK:
			printf("%s\nHF_errno = -7 ->  Block is Invalid\n", errString);
			break;
		case INVALID_RECORD_SIZE:
			printf("%s\nHF_errno = -8 ->  Invalid Record Size\n", errString);
			break;
		case INVALID_PARAMETERS:
			printf("%s\nHF_errno = -9 ->  Invalid Parameters\n", errString);
			break;
		case INVALID_SD:
			printf("%s\nHF_errno = -10 ->  Invalid ScanDesc\n", errString);
			break;
		case HAS_OPEN_SCANS:
			printf("%s\nHF_errno = %d ->  Block has open scans\n",  errString, HF_errno);
			break;
		case INVALID_FD:
			printf("%s\nHF_errno = -12 ->  Invalid FileDesc\n", errString);
			break;
		/*case REC_NOTFOUND:
			printf("%s\nHF_errno = -13 ->  Record not found\n", errString);
			break;*/
		default:
			printf("%s %d\n", errString, HF_errno);
			break;
	}
}
